package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.views.ChaPlan;
import com.xmy.cultivate.entity.views.EduTask;
import com.xmy.cultivate.entity.views.StudyPlan;
import com.xmy.cultivate.feign.math_outside.IMathematicsFeignClient;
import com.xmy.cultivate.service.*;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName MathematicsController
 * @Description: 教务管理 2024.3.22 代码版本有最初代码
 * @Version V1.0
 **/
@RestController
@RequestMapping("/api/mathematics")
public class MathematicsController extends BaseController {

    @Autowired
    @Lazy
    ICourseSchedulingDetailService iCourseSchedulingDetailService;

    @Autowired
    @Lazy
    IStudentGradeService iStudentGradeService;

    @Autowired
    @Lazy
    IMathematicsFeignClient iMathematicsFeignClient;

    @Autowired
    @Lazy
    IStaffService iStaffService;

    @Autowired
    @Lazy
    IStudyPlanService iStudyPlanService;

    @Autowired
    @Lazy
    IGradeService iGradeService;

    @Autowired
    IQuarterService iQuarterService;

    @ApiOperation("获取课次列表")
    @GetMapping("/getTaskNumber")
    public Result getTaskNumber(@RequestParam Map<String, Object> reMap) {
        System.out.printf(reMap.get("taskDate").toString()+"进来了没？");
        List<EduTask> taskNumberList = iMathematicsFeignClient.getTaskNumber(reMap);
        return new Result(Code.OK, taskNumberList,"获取课次列表");
    }

    /**
     * 任务日期-实现：先拿出分班学生数据，for拿用半角逗号分开的字符串日期，在来查本接口
     * @return
     **/
    @ApiOperation("关联课次，拿学习数量与第几课次")
    @GetMapping(value = "/getTaskList")
    public Result getTaskList(@RequestParam Map<String, Object> reMap) {
        if (reMap.get("schoolDate") == null) {
            return new Result(Code.ERROR, "任务日期schoolDate不能为空");
        }
        //任务列表
        List<EduTask> taskList = null;
        try {
            taskList = iMathematicsFeignClient.getTaskList(reMap);
            if (taskList == null || taskList.isEmpty()) {
                return new Result(Code.OK, "还没有设置任务哦，请先设置任务", "还没有设置任务哦，请先设置任务");
            }
        } catch (Exception ignored) {
        }
        //未完成
        ArrayList<Long> courseSchedulingDetailIdList = new ArrayList<>();
        List<ChaPlan> chaPlanList = null;
        try {
            chaPlanList = iMathematicsFeignClient.getChaPlanList(reMap);
            for (ChaPlan chaPlan : chaPlanList) {
                courseSchedulingDetailIdList.add(chaPlan.getStudentGradeId());
            }
        } catch (Exception ignored) {
        }
        //已完成
//        ArrayList<Long> courseSchedulingDetailIdListYesIsDate = new ArrayList<>();
        ArrayList<Long> courseSchedulingDetailIdListYesLtDate = new ArrayList<>();
        List<ChaPlan> getChaPlanListYes;
        try {
            getChaPlanListYes = iMathematicsFeignClient.getChaPlanListYes(reMap);
            for (ChaPlan chaPlan : getChaPlanListYes) {
                if (chaPlan.getSchoolDate().equals(reMap.get("schoolDate"))) {
//                    courseSchedulingDetailIdListYesIsDate.add(chaPlan.getStudentGradeId());
                } else {
                    courseSchedulingDetailIdListYesLtDate.add(chaPlan.getStudentGradeId());
                }
            }
        } catch (Exception ignored) {
            return new Result(Code.ERROR, "已完成的数据量过大");
        }
        int pageNum = StringUtils.isEmpty((String)reMap.get("pageNum")) ? 1 : Integer.parseInt((String)reMap.get("pageNum"));
        int pageSize = StringUtils.isEmpty((String)reMap.get("pageSize")) ? 100: Integer.parseInt((String)reMap.get("pageSize"));

        String field = "";

//            校区 schoolId
//            教师 teacherId
//            班级 gradeId
//            年级 yearClassId
//            学段 gradeSection

//            状态 status 做不了，不是一个库里的表
//            班型 courseTypeId 待定
        String idOr = "";
        if (!courseSchedulingDetailIdList.isEmpty()) {
            idOr = " OR sg.id IN ("+courseSchedulingDetailIdList.stream().map(Object::toString).collect(Collectors.joining(","))+") ";
        }
        String yearClassIdString = "";
        //年级
        if (reMap.get("yearClassId") != null && !reMap.get("yearClassId").toString().isEmpty()) {
            yearClassIdString = reMap.get("yearClassId").toString();
        }
        //学段
        if (reMap.get("gradeSection") != null && !reMap.get("gradeSection").toString().isEmpty()) {
            yearClassIdString = reMap.get("gradeSection").toString();
        }

        String whereSql = "";
        if (!courseSchedulingDetailIdListYesLtDate.isEmpty()) {
            whereSql += " AND sg.id NOT IN ("+ courseSchedulingDetailIdListYesLtDate.stream().map(Object::toString).collect(Collectors.joining(",")) +") ";
        }
        if (reMap.get("schoolId") != null && !reMap.get("schoolId").toString().isEmpty()) {
            whereSql += " AND csd.school_id = "+ reMap.get("schoolId");
        }
        if (reMap.get("teacherId") != null && !reMap.get("teacherId").toString().isEmpty()) {
            whereSql += " AND csd.teacher_id = "+ reMap.get("teacherId");
        }
        if (reMap.get("gradeId") != null && !reMap.get("gradeId").toString().isEmpty()) {
            whereSql += " AND csd.grade_id = "+ reMap.get("gradeId");
        }
        whereSql += " AND sg.`status`<>3 AND sg.subjects_id =2 ";
        StringBuilder s = new StringBuilder();
        Calendar calendar = Calendar.getInstance();

        if (taskList != null) {
            for (EduTask eduTask: taskList) {
                if (eduTask.getTaskDate() != null) {
                    String schoolDate = eduTask.getTaskDate().toString();
                    String[] date = schoolDate.split("-");
                    calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1]) - 1, Integer.parseInt(date[2]));
                }
                int monthPart = calendar.get(calendar.MONTH);//当前月份
                int yearPart = calendar.get(calendar.YEAR);//当前年份
                int todayPart = calendar.get(calendar.DAY_OF_MONTH);//当前天数
                int week = calendar.get(calendar.DAY_OF_WEEK);
                if (week == 1) {
                    calendar.set(yearPart, monthPart, (todayPart - 6));
                } else {
                    calendar.set(yearPart, monthPart, (todayPart - (week - 2)));
                }
                int todayPartStart = calendar.get(calendar.DAY_OF_MONTH);//开始天数
                int monthPartStart = calendar.get(calendar.MONTH) + 1;//开始月份
                int yearPartStart = calendar.get(calendar.YEAR);//开始年份
                if (week == 1) {
                    calendar.set(yearPart, monthPart, todayPart);
                } else {
                    calendar.set(yearPart, monthPart, (todayPart + (7 - week) + 1));
                }
                int todayPartEnd = calendar.get(calendar.DAY_OF_MONTH);//结束天数
                int monthPartEnd = calendar.get(calendar.MONTH) + 1;//结束月份
                int yearPartEnd = calendar.get(calendar.YEAR);//结束年份

                String school_date = " AND csd.school_date between '"+ yearPartStart+"-"+monthPartStart+"-"+todayPartStart+" 00:00:00' AND '"+yearPartEnd+"-"+monthPartEnd+"-"+todayPartEnd+" 23:59:59' ";
                if (s.length() > 0) {
                    s.append(" UNION ");
                }
                String isYearClassId = yearClassIdString;
                if (Objects.equals(isYearClassId, "")) {
                    if (eduTask.getYearClassId() != null) {
                        isYearClassId = eduTask.getYearClassId().toString();
                    }
                }
                String lessonNumIs = null;
                if (eduTask.getTaskCourseNum() != null) {
                    lessonNumIs = eduTask.getTaskCourseNum().toString();
                }
                String quarterNumSql = null;
                if (eduTask.getQuarterNum() != null) {
                    quarterNumSql = "AND sg.quarter_num = "+ eduTask.getQuarterNum() +" ";
                }
                String year_part_sql_vo = null;
                if (eduTask.getYearPart() != null) {
                    year_part_sql_vo = eduTask.getYearPart().toString();
                }
                String year_part_sql = " AND csd.year_part = "+ year_part_sql_vo +" AND sg.year_part = "+ year_part_sql_vo + " AND sg2.year_part = "+ year_part_sql_vo +" ";
                s.append(" (SELECT sg.id,sg.order_detail_id,sg.grade_id,sg.lesson_type,sg.course_id,sg.subjects_id,sg2.year_class_id,sg.student_id,sg.created_id,sg.created_at" +
                                ",sg.updated_at,sg.school_id,sg.deleted_at,sg.end_date,sg.use_course_count,sg.status,sg.reading_status,sg.renew_teacher_id,sg.year_part,sg.quarter_num,sg.surplus_course_num" +
                                ",sg.is_renew,sg.stop_course_date,sg.course_status,sg.is_presort,sg.is_last" +
                                ",csd.school_date,csd.lesson_num,sg2.grade_id AS sg2_grade_id ")
                        .append(field).append(" FROM student_grade AS sg ")
                        .append("INNER JOIN course_scheduling_detail AS csd ON csd.grade_id = sg.grade_id AND csd.lesson_type = 3 AND csd.course_id = sg.course_id AND csd.school_id = sg.school_id ").append(school_date)
                        .append("AND csd.lesson_num = ")
                        .append(lessonNumIs).append(" ")
                        .append("AND csd.deleted = 0 ")
                        .append("INNER JOIN student_grade AS sg2 ON sg2.student_id = sg.student_id AND sg2.lesson_type = 1 AND sg2.year_class_id = ")
                        .append(isYearClassId)
                        .append(" AND sg2.deleted = 0 AND sg2.`status`<>3 AND sg2.subjects_id =2 AND sg2.is_last=1 ");
                        //班型
                        if (reMap.get("courseTypeId") != null && !reMap.get("courseTypeId").toString().isEmpty()) {
                            s.append("INNER JOIN grade AS g ON g.id = sg2.grade_id AND g.lesson_type = 1 AND g.year_class_id = sg2.year_class_id AND g.course_type_id = ")
                                    .append(reMap.get("courseTypeId"))
                                    .append(" AND g.quarter_num = sg2.quarter_num AND g.year_part = sg2.year_part AND g.deleted = 0 AND g.status <> 3 ");
                        }
                        s.append("WHERE (sg.lesson_type = 3 AND sg.deleted = 0 ").append(whereSql).append(quarterNumSql).append(year_part_sql).append(") ").append(idOr).append(" GROUP BY sg.student_id ").append(" ORDER BY sg.student_id,csd.school_date ASC) ");
            }
        }

        String countSql = "SELECT COUNT(*) AS id FROM (" + s + ") AS t";
        List<StudentGrade> countList = iStudentGradeService.getTaskList(countSql);
        long count = countList.get(0).getId();
        long l = count / pageSize;
        if (count % pageSize > 0) {
            l += 1L;
        }
        String pageSql = " LIMIT " + ((pageNum - 1) * pageSize) + ", " + pageSize;
        String pageList = "SELECT t.*,(SELECT name FROM student WHERE id = t.student_id LIMIT 1) AS studentName FROM (" + s + ") AS t ORDER BY t.school_date ASC " + pageSql;
        List<StudentGrade> taskList1 = iStudentGradeService.getTaskList(pageList);
        for (StudentGrade studentGrade: taskList1) {
            studentGrade.setTaskCourseNum(0);//学习到的课次 先设置默认值
            studentGrade.setLowestShutnum(0);//应通关数 先设置默认值
            if (taskList != null) {
                for (EduTask eduTask: taskList) {

                    if (studentGrade.getYearClassId().equals(eduTask.getYearClassId())
                            && studentGrade.getQuarterNum().equals(eduTask.getQuarterNum())
                            && studentGrade.getYearPart().equals(eduTask.getYearPart())
                            && studentGrade.getLessonNum().equals(eduTask.getTaskCourseNum())
                            && (studentGrade.getSchoolDate().equals(eduTask.getTaskDate()) || isDateTime(eduTask.getTaskDate().toString(), studentGrade.getSchoolDate().toString()))
                    ) {
                        studentGrade.setTaskCourseNum(eduTask.getTaskCourseNum());//学习到的课次
                        studentGrade.setLowestShutnum(eduTask.getLowestShutnum());//应通关数
                    }
                }
            }
            studentGrade.setRightNum(0);//正确题数 先设置默认值
            studentGrade.setAmendNum(0);//修正题数 先设置默认值
            studentGrade.setInputStatus(0);//录入状态 0未录入；1已录入
            //未完成
            if (chaPlanList != null) {
                for (ChaPlan chaPlan : chaPlanList) {
                    if (chaPlan.getStudentGradeId().equals(studentGrade.getId())) {
                        studentGrade.setRightNum(chaPlan.getRightNum());//正确题数
                        studentGrade.setAmendNum(chaPlan.getAmendNum());//修正题数
                        studentGrade.setInputStatus(1);//录入状态 0未录入；1已录入
                    }
                }
            }
            //已完成
            for (ChaPlan chaPlan : getChaPlanListYes) {
                if (chaPlan.getStudentGradeId().equals(studentGrade.getId())) {
                    studentGrade.setRightNum(chaPlan.getRightNum());//正确题数
                    studentGrade.setAmendNum(chaPlan.getAmendNum());//修正题数
                    studentGrade.setInputStatus(1);//录入状态 0未录入；1已录入
                }
            }
        }
        return new Result(Code.OK, new PageResult<>(taskList1,count,l),"获取课次列表");
    }
    private Boolean isDateTime(String schoolDate, String isDate) {
        Calendar calendar = Calendar.getInstance();
        String[] date = schoolDate.split("-");
        calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1]) - 1, Integer.parseInt(date[2]));
        int monthPart = calendar.get(calendar.MONTH);//当前月份
        int yearPart = calendar.get(calendar.YEAR);//当前年份
        int todayPart = calendar.get(calendar.DAY_OF_MONTH);//当前天数
        int week = calendar.get(calendar.DAY_OF_WEEK);
        if (week == 1) {
            calendar.set(yearPart, monthPart, (todayPart - 6));
        } else {
            calendar.set(yearPart, monthPart, (todayPart - (week - 2)));
        }
        int todayPartStart = calendar.get(calendar.DAY_OF_MONTH);//开始天数
        int monthPartStart = calendar.get(calendar.MONTH) + 1;//开始月份
        int yearPartStart = calendar.get(calendar.YEAR);//开始年份
        String oenDate = yearPartStart + "-" + monthPartStart + "-" + todayPartStart;
        if (isDate.equals(oenDate)) {
            return true;
        }
        if (week == 1) {
            calendar.set(yearPart, monthPart, todayPart);
        } else {
            calendar.set(yearPart, monthPart, (todayPart + (7 - week) + 1));
        }
        int todayPartEnd = calendar.get(calendar.DAY_OF_MONTH);//结束天数
        int monthPartEnd = calendar.get(calendar.MONTH) + 1;//结束月份
        int yearPartEnd = calendar.get(calendar.YEAR);//结束年份
        String oenDateEnd = yearPartEnd + "-" + monthPartEnd + "-" + todayPartEnd;
        if (isDate.equals(oenDateEnd)) {
            return true;
        }
        return dateToWeek(oenDate, isDate);
    }
    private Boolean dateToWeek(String schoolDate, String isDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
        Date date = null; // 指定的日期
        try {
            date = sdf.parse(schoolDate);
        } catch (ParseException ignored) {
        }
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }

        // 打印指定日期所在周的所有日期
        for (int i = 2; i < 8; i++) {
            // 调整为一周中的某一天（0=星期天，1=星期一，以此类推）
            calendar.set(calendar.DAY_OF_WEEK, i);
            if (sdf.format(calendar.getTime()).equals(isDate)) {
                return true;
            }
        }
        return false;
    }

    @ApiOperation("查看老师列表")
    @GetMapping("/searchOne")
    public Result searchOne(@RequestParam(value = "name",required = false) String name,
                            @RequestParam(value = "schoolId",required = false) String schoolId,
                            @RequestParam(value = "yearClassId",required = false) String yearClassId,
                            @RequestParam(value = "subjectsId",required = false,defaultValue = "2") String subjectsId,
                            @RequestParam(value = "teacherIdList", defaultValue = "") Object[] teacherIdList) {

        QueryWrapper<Staff> staffQueryWrapper = new QueryWrapper<>();
        staffQueryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        if (teacherIdList.length > 0) {
            staffQueryWrapper.notIn("id", teacherIdList);
        }

        Quarter quarterNow = iQuarterService.getNowQuarter();

        String sql = "SELECT t1.teacher_id FROM grade as t1 left join course as t2 on t1.course_id = t2.id WHERE t1.lesson_type = 1 and t2.subjects_id = "+subjectsId+" AND t1.deleted = 0 and t1.year_part="+quarterNow.getYearPart()+" and t1.quarter_num = "+quarterNow.getNum();
        if (StringUtils.isNotBlank(schoolId)){
            sql += " and t1.school_id="+schoolId;
        }
        if (StringUtils.isNotBlank(yearClassId)){
            sql += " and t1.year_class_id="+yearClassId;
        }
        staffQueryWrapper.inSql("id",sql);
        List<Staff> staffList = iStaffService.list(staffQueryWrapper);
        return new Result(Code.OK, new PageResult<>(staffList, (long) staffList.size(), 1L));

    }

    @ApiOperation("保存数量：正确、修正")
    @PostMapping("/saveChaPlan")
    public Result saveChaPlan(@RequestBody Map<String, Object> reMap) {
        if (reMap.get("id") == null || reMap.get("id").toString().isEmpty()) {
            return new Result(Code.ERROR, "id不能为空", "id不能为空");
        }
        if (reMap.get("schoolDate") == null || reMap.get("schoolDate").toString().isEmpty()) {
            return new Result(Code.ERROR, "id不能为空", "schoolDate不能为空");
        }
        String schoolDate = reMap.get("schoolDate").toString();
        Long studentGradeId = Long.valueOf(reMap.get("id").toString());
        StudentGrade studentGradeServiceById = iStudentGradeService.getById(studentGradeId);
        QueryWrapper<CourseSchedulingDetail> courseSchedulingDetailQueryWrapper = new QueryWrapper<>();
        courseSchedulingDetailQueryWrapper.eq("grade_id",studentGradeServiceById.getGradeId());
//        courseSchedulingDetailQueryWrapper.eq("course_id",studentGradeServiceById.getCourseId());
        courseSchedulingDetailQueryWrapper.eq("lesson_type",3);
        courseSchedulingDetailQueryWrapper.eq("school_id",studentGradeServiceById.getSchoolId());
        courseSchedulingDetailQueryWrapper.eq("year_part",studentGradeServiceById.getYearPart());
        courseSchedulingDetailQueryWrapper.eq("deleted",0);
        Calendar calendar = Calendar.getInstance();
        if (reMap.get("schoolDate") != null) {
            String[] date = schoolDate.split("-");
            calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1]) - 1, Integer.parseInt(date[2]));
        }
        int monthPart = calendar.get(calendar.MONTH);//当前月份
        int yearPart = calendar.get(calendar.YEAR);//当前年份
        int todayPart = calendar.get(calendar.DAY_OF_MONTH);//当前天数
        int week = calendar.get(calendar.DAY_OF_WEEK);
        if (week == 1) {
            calendar.set(yearPart, monthPart, (todayPart - 6));
        } else {
            calendar.set(yearPart, monthPart, (todayPart - (week - 2)));
        }
        int todayPartStart = calendar.get(calendar.DAY_OF_MONTH);//开始天数
        int monthPartStart = calendar.get(calendar.MONTH) + 1;//开始月份
        int yearPartStart = calendar.get(calendar.YEAR);//开始年份
        if (week == 1) {
            calendar.set(yearPart, monthPart, todayPart);
        } else {
            calendar.set(yearPart, monthPart, (todayPart + (7 - week) + 1));
        }
        int todayPartEnd = calendar.get(calendar.DAY_OF_MONTH);//结束天数
        int monthPartEnd = calendar.get(calendar.MONTH) + 1;//结束月份
        int yearPartEnd = calendar.get(calendar.YEAR);//结束年份
        courseSchedulingDetailQueryWrapper.between("school_date", schoolDate+" 00:00:00",yearPartEnd+"-"+monthPartEnd+"-"+todayPartEnd+" 23:59:59");
//        courseSchedulingDetailQueryWrapper.eq("school_date",schoolDate);
        courseSchedulingDetailQueryWrapper.orderByAsc("school_date");
//        courseSchedulingDetailQueryWrapper.last("LIMIT 1");
        List<CourseSchedulingDetail> courseSchedulingDetails = iCourseSchedulingDetailService.list(courseSchedulingDetailQueryWrapper);
        Boolean b = false;
        for (CourseSchedulingDetail courseSchedulingDetailServiceOne: courseSchedulingDetails) {
            QueryWrapper<StudentGrade> studentGradeQueryWrapper = new QueryWrapper<>();
            studentGradeQueryWrapper.eq("grade_id",courseSchedulingDetailServiceOne.getGradeId());
            studentGradeQueryWrapper.eq("lesson_type",3);
            studentGradeQueryWrapper.eq("school_id",courseSchedulingDetailServiceOne.getSchoolId());
            studentGradeQueryWrapper.eq("deleted",0);
            studentGradeQueryWrapper.eq("student_id",studentGradeServiceById.getStudentId());
            studentGradeQueryWrapper.eq("quarter_num",studentGradeServiceById.getQuarterNum());
            studentGradeQueryWrapper.eq("year_class_id",studentGradeServiceById.getYearClassId());
            studentGradeQueryWrapper.eq("year_part",studentGradeServiceById.getYearPart());
            studentGradeQueryWrapper.last("LIMIT 1");
            StudentGrade studentGradeServiceOne = iStudentGradeService.getOne(studentGradeQueryWrapper);
            ChaPlan chaPlan = new ChaPlan();
            chaPlan.setAmendNum(Integer.valueOf(reMap.get("amendNum").toString()));
            chaPlan.setRightNum(Integer.valueOf(reMap.get("rightNum").toString()));
            chaPlan.setStudentGradeId(studentGradeServiceOne.getId());
            chaPlan.setSchoolDate(courseSchedulingDetailServiceOne.getSchoolDate());
            chaPlan.setSchoolId(studentGradeServiceOne.getSchoolId());
            chaPlan.setStudentId(studentGradeServiceOne.getStudentId());
            chaPlan.setQuarterNum(studentGradeServiceOne.getQuarterNum());
            chaPlan.setYearPart(studentGradeServiceOne.getYearPart());
            chaPlan.setTeacherId(courseSchedulingDetailServiceOne.getTeacherId());
            chaPlan.setGradeId(courseSchedulingDetailServiceOne.getGradeId());
            QueryWrapper<StudentGrade> studentGradeQueryWrapper1 = new QueryWrapper<>();
            studentGradeQueryWrapper1.eq("lesson_type",1);
            studentGradeQueryWrapper1.eq("subjects_id",2);
            studentGradeQueryWrapper1.eq("is_last",1);
            studentGradeQueryWrapper1.ne("status",3);
            studentGradeQueryWrapper1.eq("student_id",studentGradeServiceOne.getStudentId());
            studentGradeQueryWrapper1.eq("quarter_num",studentGradeServiceOne.getQuarterNum());
            studentGradeQueryWrapper1.eq("year_part",studentGradeServiceOne.getYearPart());
            studentGradeQueryWrapper1.eq("deleted",0);
            studentGradeQueryWrapper1.last("LIMIT 1");
            StudentGrade studentGradeServiceOne1 = iStudentGradeService.getOne(studentGradeQueryWrapper1);
            if (studentGradeServiceOne1 != null) {
                chaPlan.setYearClassId(studentGradeServiceOne1.getYearClassId());
                Grade gradeServiceById = iGradeService.getById(studentGradeServiceOne1.getGradeId());
                if (gradeServiceById != null) {
                    chaPlan.setCourseTypeId(gradeServiceById.getCourseTypeId());
                }
            }
            b = iMathematicsFeignClient.saveChaPlan(chaPlan);
        }
        if (b) {
            return new Result(Code.OK, "", "保存成功");
        }
        return new Result(Code.ERROR, "", "保存失败");
    }


    @ApiOperation("通关率列表-查询期间为当前周一至周日")
    @GetMapping("getClearanceRateChaPlan")
    public Result getClearanceRateChaPlan(@RequestParam Map<String, Object> reMap) {
        List<StudyPlan> studyPlanList = iStudyPlanService.getClearanceRateChaPlan(reMap);
        return new Result(Code.OK, new PageResult<>(studyPlanList, (long) studyPlanList.size(), 1L), "获取通关率列表成功");
    }

    @ApiOperation("通关率详情-查询期间为当前周一至周日")
    @PostMapping("getClearanceRateChaPlanDetail")
    public Result getClearanceRateChaPlanDetail(@RequestBody Map<String, Object> reMap) {
        IPage<StudyPlan> iPage = iStudyPlanService.getClearanceRateChaPlanDetail(reMap);
        return new Result(Code.OK, new PageResult<>(iPage.getRecords(), iPage.getTotal(), iPage.getPages()), "获取通关率详情成功");
    }
}
